package com.lws.systembarsample

import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.updateLayoutParams

class FullScreenMarginActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_full_screen_inset)

        ViewCompat.getWindowInsetsController(window.decorView)?.isAppearanceLightStatusBars = true
        ViewCompat.getWindowInsetsController(window.decorView)?.isAppearanceLightNavigationBars = true


        //设置decorView全屏显示,即显示区域延伸至状态栏、导航栏
        WindowCompat.setDecorFitsSystemWindows(window, false)


        val topTextView: TextView = findViewById(R.id.topTextView)
        ViewCompat.setOnApplyWindowInsetsListener(topTextView) { view: View, windowInsetsCompat: WindowInsetsCompat ->

            //给这个View通过设置 Margin 或者 padding ,使该View不被状态栏遮挡
            //这里以修改Margin为例
            view.updateLayoutParams<ConstraintLayout.LayoutParams> {
                topMargin = windowInsetsCompat.getInsets(WindowInsetsCompat.Type.statusBars()).top
            }

            //返回原来的Inset，以便它们继续向下传递
            windowInsetsCompat
        }


        val bottomTextView: TextView = findViewById(R.id.bottomTextView)
        ViewCompat.setOnApplyWindowInsetsListener(bottomTextView) { view: View, windowInsetsCompat: WindowInsetsCompat ->

            //给这个View通过设置 Margin 或者 padding ,使该View不被导航栏遮挡
            //这里以修改Margin为例
            view.updateLayoutParams<ConstraintLayout.LayoutParams> {
                bottomMargin =
                    windowInsetsCompat.getInsets(WindowInsetsCompat.Type.navigationBars()).bottom
            }
            //返回原来的Inset，以便它们继续向下传递
            windowInsetsCompat
        }
    }
}